/**
 * @file	gpio.h
 * @author	chipsea
 * @brief	
 * @version	0.1
 * @date	2020-11-30
 * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
 * @note
 */

/*******************************************************************************
* @file		gpio.h
* @brief	Contains all functions support for gpio and iomux driver
* @version	0.0
* @date		19. Oct. 2017
* @author	qing.han
* 

*
*******************************************************************************/
#ifndef __GPIO_H__
#define __GPIO_H__

#ifdef __cplusplus
extern "C" {
#endif
	
#include <types.h>
#include "cst92f2x.h"
#include "error.h"

#define NUMBER_OF_PINS      23

typedef enum{
    GPIO_P00   =   0,    P0  =  GPIO_P00,
    GPIO_P01   =   1,    P1  =  GPIO_P01,
    GPIO_P02   =   2,    P2  =  GPIO_P02,
    GPIO_P03   =   3,    P3  =  GPIO_P03,
    GPIO_P07   =   4,    P7  =  GPIO_P07, 
    GPIO_P09   =   5,    P9  =  GPIO_P09,
    GPIO_P10   =   6,    P10  =  GPIO_P10,
    GPIO_P11   =   7,    P11  =  GPIO_P11,   Analog_IO_0 = GPIO_P11,
    GPIO_P14   =   8,    P14  =  GPIO_P14,   Analog_IO_1 = GPIO_P14,
    GPIO_P15   =   9,    P15  =  GPIO_P15,   Analog_IO_2 = GPIO_P15,
    GPIO_P16   =   10,   P16  =  GPIO_P16,   Analog_IO_3 = GPIO_P16,XTALI = GPIO_P16,
    GPIO_P17   =   11,   P17  =  GPIO_P17,   Analog_IO_4 = GPIO_P17,XTALO = GPIO_P17,
    GPIO_P18   =   12,   P18  =  GPIO_P18,   Analog_IO_5 = GPIO_P18,
    GPIO_P20   =   13,   P20  =  GPIO_P20,   Analog_IO_6 = GPIO_P20,
    GPIO_P23   =   14,   P23  =  GPIO_P23,   Analog_IO_7 = GPIO_P23,
    GPIO_P24   =   15,   P24  =  GPIO_P24,   Analog_IO_8 = GPIO_P24,
    GPIO_P25   =   16,   P25  =  GPIO_P25,   Analog_IO_9 = GPIO_P25,
    GPIO_P26   =   17,   P26  =  GPIO_P26,
    GPIO_P27   =   18,   P27  =  GPIO_P27,
    GPIO_P31   =   19,   P31  =  GPIO_P31,
    GPIO_P32   =   20,   P32  =  GPIO_P32,
    GPIO_P33   =   21,   P33  =  GPIO_P33,
    GPIO_P34   =   22,   P34  =  GPIO_P34,
    GPIO_NUM   =   23,
	GPIO_BOOT  =   0xFE,
    GPIO_DUMMY =  0xff,
}GpioPin_t;

typedef enum {  
    FMUX_IIC0_SCL= 0,
    FMUX_IIC0_SDA= 1,
    FMUX_IIC1_SCL= 2,
    FMUX_IIC1_SDA= 3,
    FMUX_UART0_TX=4,        FMUX_UART_TX=4,
    FMUX_UART0_RX=5,        FMUX_UART_RX=5,
    FMUX_RF_RX_EN=6,
    FMUX_RF_TX_EN=7,
    FMUX_UART1_TX=8,
    FMUX_UART1_RX=9,
    FMUX_PWM0=10,
    FMUX_PWM1=11,
    FMUX_PWM2=12,
    FMUX_PWM3=13,
    FMUX_PWM4=14,
    FMUX_PWM5=15, 
    FMUX_SPI_0_SCK=16,
    FMUX_SPI_0_SSN=17,
    FMUX_SPI_0_TX=18, 
    FMUX_SPI_0_RX=19, 
    FMUX_SPI_1_SCK=20,
    FMUX_SPI_1_SSN=21,
    FMUX_SPI_1_TX=22, 
    FMUX_SPI_1_RX=23, 
    FMUX_CHAX=24,    
    FMUX_CHBX=25, 
    FMUX_CHIX=26, 
    FMUX_CHAY=27,
    FMUX_CHBY=28, 
    FMUX_CHIY=29, 
    FMUX_CHAZ=30, 
    FMUX_CHBZ=31, 
    FMUX_CHIZ=32, 
    FMUX_CLK1P28M=33,
    FMUX_ADCC=34,
    FMUX_ANT_SEL_0=35,
    FMUX_ANT_SEL_1=36,
    FMUX_ANT_SEL_2=37,

}gpio_fmux_e;
 
typedef enum{
    FRE_HCLK_DIV8 = 0,	//p0
    FRE_PCLK_DIV4 = 1,	//p1
    FRE_CLK_1P28M = 2,	//p2
    FRE_CLK_RC32K = 6,	//p24
    FRE_XTAL_CLK32768 = 7,	//p25
}Freq_Type_e;

typedef enum{
    GPIO_INPUT  = 0,
    GPIO_OUTPUT = 1
}gpio_dir_e;

typedef enum{
    POL_FALLING = 0,    POL_ACT_LOW  = 0,
    POL_RISING  = 1,    POL_ACT_HIGH = 1
}gpio_polarity_e;

typedef enum{
    Bit_DISABLE = 0,
    Bit_ENABLE,
}bit_action_e;

typedef enum{
    GPIO_FLOATING   = 0x00,     //no pull
	GPIO_PULL_UP_S  = 0x01,     //pull up strong
    GPIO_PULL_UP    = 0x02,     //pull up weak
    GPIO_PULL_DOWN  = 0x03,
}gpio_pupd_e;

typedef struct{
  GpioPin_t pin;
  gpio_pupd_e type;
}ioinit_cfg_t;


#define NEGEDGE             POL_FALLING
#define POSEDGE             POL_RISING
#define IO_Wakeup_Pol_e     gpio_polarity_e

#define FLOATING            GPIO_FLOATING
#define WEAK_PULL_UP        GPIO_PULL_UP
#define STRONG_PULL_UP      GPIO_PULL_UP_S
#define PULL_DOWN           GPIO_PULL_DOWN
#define GpioPin_t          GpioPin_t
#define OEN                 GPIO_OUTPUT
#define IE                  GPIO_INPUT
#define Fmux_Type_e         gpio_fmux_e
#define GPIO_Wakeup_Pol_e   gpio_polarity_e
#define BitAction_e         bit_action_e
typedef void (*gpioin_Hdl_t)(GpioPin_t pin,gpio_polarity_e type);




//rom api
extern int gpio_write(GpioPin_t pin, uint8_t en);
int hal_gpioin_disable(GpioPin_t pin);


void HalGpioIRQHandler(void);
void HalGpioPin2Pin3Control(GpioPin_t pin, uint8_t en);//0:sw,1:other func
bool HalBootPinStatusGet(void);
void HalGpioSet(GpioPin_t pin, bit_action_e bitVal);
bool HalGpioGet(GpioPin_t pin);
void HalGpioDsControl(GpioPin_t pin, bit_action_e value);
ErrCode_t HalGpioAnalogConfig(GpioPin_t pin, bit_action_e value);
ErrCode_t HalGpioPupdConfig(GpioPin_t pin, gpio_pupd_e type);
ErrCode_t HalGpioFmuxEnable(GpioPin_t pin, bit_action_e value);
ErrCode_t HalGpioFmuxConfig(GpioPin_t pin, gpio_fmux_e type);
ErrCode_t HalGpioWkupConfig(GpioPin_t pin, gpio_polarity_e type);
ErrCode_t HalGpioRegister(GpioPin_t pin, gpioin_Hdl_t posedgeHdl, gpioin_Hdl_t negedgeHdl);
ErrCode_t HalGpioUnregister(GpioPin_t pin);
ErrCode_t HalGpioInit(void);
ErrCode_t HalGpioPinInit(GpioPin_t pin, gpio_dir_e type);

#ifdef __cplusplus
}
#endif


#endif
